﻿using Report_Dal.Web;
using Report_IDal.Web;
using Report_IService.Web;
using Report_Model.Db;
using Report_Model.Web;
using Report_Service.Basic;
using Report_Unity.Basic;
using Report_Unity.Unitys;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Report_Service.Web
{
    public class AdminService :BaseService<T_Ticket_Record>,IAdminService
    {
        private IT_HospitalDal T_HospitalService = new T_HospitalDal(ConfigSource.tab_web);
        private IT_Ticket_RecordDal T_Ticket_RecordDal = new T_Ticket_RecordDal(ConfigSource.tab_web);

        public override void SetCurrentDal()
        {
            base.BaseDal = T_Ticket_RecordDal;
        }


        public ResultInfo LoginSuper(string username, string pwd,string ticket,string code)
        {
            if (string.IsNullOrEmpty(username))
                return CodeStatus.error_201.GetStatus($"{nameof(username)}必传");

            if (string.IsNullOrEmpty(pwd))
                return CodeStatus.error_201.GetStatus($"{nameof(pwd)}必传");

            if (string.IsNullOrEmpty(ticket))
                return CodeStatus.error_201.GetStatus($"{nameof(ticket)}必传");
            Guid gid;
            if (!Guid.TryParse(ticket, out gid))
                return CodeStatus.error_201.GetStatus($"{nameof(ticket)}格式不正确");

            if (string.IsNullOrEmpty(code))
                return CodeStatus.error_201.GetStatus($"{nameof(code)}必传");

            T_Ticket_Record ticketRecord = T_Ticket_RecordDal.GetAsNoTracking(a => a.check_ticket == gid).FirstOrDefault();
            if (ticketRecord == null)
                return CodeStatus.error_203.GetStatus("失效");
            if (ticketRecord.check_code != code)
                return CodeStatus.error_203.GetStatus("不正确");

            SuperUser superUser = new SuperUser();

            if (superUser.username != username || superUser.pwd != pwd)
                return CodeStatus.error_204.GetStatus();
            object objResult = new {userid = superUser.userid,name = superUser.name,sex = superUser.sex };
            ResultInfo res = CodeStatus.success.GetStatus();
            res.data = objResult;

            return res;
        }

     
    }
}
